<?php
namespace Home\Controller;
use Think\Controller; 
class AdminController extends Controller {
	
	public function __construct() {
		parent::__construct();
		if(empty($_SESSION['user']['id']))
		{
			$this->redirect('Home/Login/login');
		}
    }
	/*public function add_test()
	{   
		$data['username']=test.rand(0,9999999);
		$data['password']=md5('123456');
		$data['type']=2;
		$User = M("User_list");
		
		for ($i = 0; $i < 450000; $i++) 
		{
			$res=$User->add($data);
		}
		
	}*/
	/**
	 * 添加管理员
	 */
    public function admin_add($id,$username,$password,$email,$phone){
		$data['username']=trim($username);
		$data['password']=md5(trim($password));
		$data['email']=trim($email);
		$data['phone']=trim($phone);
		$data['type']=2;
		$User = M("User_list");
		if(empty($id)){
			$res=$User->add($data);
			//$this->assign("res3",$res);
		}else{
			$res=$User->where('id='.$id)->save($data);
			//$this->assign("res5",$res);
			echo $res;
		}
		
		//$this->admin_list();
	}
	/**
	* 管理员列表
	*
	*/
	public function admin_list(){
		$User = M("User_list"); // 实例化User对象
		$res=$User->where('type=2')->order('id desc')->limit(10)->select(); 
		$count['count']=$User->where('type=2')->count();
		$count['pageNo']=1;
		$count['pageSize']=10;
		$count['countpage']=ceil($count['count']/$count['pageSize']);
		$order['name']='id';
		$order['asc']='desc';
		//var_dump($count);die();
		$this->assign("order",$order);
		$this->assign("res",$res);
		$this->assign("count",$count);
		$this->display('admin_list'); 
    }
	//
	public function admin_info(){
		$this->display('admin_info');
    }
	/*public function delete($id){
		if(!empty($id)){
			$User = M("User_list");
			$res=$User->where('id='.$id)->delete();
			if($res){
				$this->assign("res2",$res);
				$this->admin_list();
			}
		}
    }*/
	/**
	 * 编辑管理员编辑信息
	 */
	public function update($id){
		if(!empty($id)){
			$User = M("User_list");
			$res=$User->where('id='.$id)->find();
			$this->assign("res4",$res);
			$this->admin_info();
		}
    }
	/*public function find($username,$phone,$email){
		if(!empty($username)){
			$username=trim($username);
			$where['username'] = array('like',"%$username%");
			$data['username']=$username;
			
		}
		if(!empty($phone)){
			$phone=trim($phone);
			$where['phone'] = array('like',"%$phone%");
			$data['phone']=$phone;
		}
		if(!empty($email)){
			$email=trim($email);
			$where['email'] = array('like',"%$email%");
			$data['email']=$email;
		}
		$where['type']=2;
		$User = M("User_list"); 
		$res=$User->where($where)->select(); 
		$count['count']=$User->where($where)->count();
		$count['pageNo']=1;
		$count['pageSize']=10;
		$count['countpage']=ceil($count['count']/$count['pageSize']);
		$this->assign("count",$count);
		$this->assign("data",$data);
		$this->assign("res",$res);
		$this->display('admin_list');
    }*/
	public function find($pageNo,$pageSize,$username,$phone,$email,$name,$asc){
		if(!empty($username)){
			$username=trim($username);
			$where['username'] = array('like',"%$username%");
			$data['username']=$username;
			
		}
		if(!empty($phone)){
			$username=trim($phone);
			$where['phone'] = array('like',"%$phone%");
			$data['phone']=$phone;
		}
		if(!empty($email)){
			$username=trim($email);
			$where['email'] = array('like',"%$email%");
			$data['email']=$email;
		}
		$where['type']=2;
		
		if(empty($name)){
			$name='id';
		}
		if(empty($asc)){
			$asc='desc';
		}
		
		$User = M("User_list"); 
		$count['count']=$User->where($where)->count();
		$count['countpage']=ceil($count['count']/$pageSize);
		if($pageNo>=$count['countpage']){
			$pageNo=$count['countpage'];
		}
		if($pageNo<=0){
			$pageNo=1;
		}
		
		$res=$User->where($where)->field('id,username,phone,email')->order("$name $asc")->limit(($pageNo-1)*$pageSize,$pageSize)->select(); 
		//echo $res->getLastSql();
		
		/*foreach($res as $key=> $value){
			$newwhere['id']=$value['id'];
			$newres[$key]=$User->where($newwhere)->field('id,username,phone,email')->find();
		}*/
		$count['pageNo']=$pageNo;
		$count['pageSize']=$pageSize;
		$count['num']=$count['countpage']-4;
		$order['name']=$name;
		$order['asc']=$asc;
		$this->assign("order",$order);
		$this->assign("data",$data);
		$this->assign("res",$res);
		$this->assign("count",$count);
		$this->display('admin_list'); 
	}
	public function delete($id){
		if(!empty($id)){
			$User = M("User_list");
			$res=$User->where('id='.$id)->delete();
			echo $res;die();
			/*if($res){
				$this->assign("res2",$res);
				$this->user_list();
			}*/
		}
    }
	/**
	 * 管理员新增与修改
	 */
	public function admin_update($id,$username,$password,$email,$phone){
		$User = M("User_list");
		if(!empty($password)){
			$data['password'] = md5(trim($password));
		}
		
		//数据接收
		$data['username']= trim($username);
		$data['email']	 = trim($email);
		$data['phone']	 = trim($phone);
		$data['type']	 = 2;
		
		//新增管理员信息
		if(empty($id)){
			$row=$User->where("username='".$data['username']."'")->find();
			if($row){
				echo 4;die();   //用户已存在
			}else{
				$res=$User->add($data);
				if($res){
					echo 1;die();//新增成功
				}else{
					echo 0;die();//新增失败
				}
			}
		//编辑管理员信息
		}else{
			$info = $User->where("id=".$id)->find();//当前用户id查找			
			$row = $User->where("username='".$data['username']."' and username != '".$info['username']."'")->find();
			//为true时,用户名存在
			if($row){
				echo 4;die();
			}else{
				$res = $User->where('id='.$id)->save($data);				
				if($res > 0){
					echo 2;//修改成功
				}else{
					echo 3;//修改失败
				}die();
			}
		}
	}
}
















